package com.software.levelupandroid.home.ui.taskByPriority.adapter;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.software.levelupandroid.R;
import com.software.levelupandroid.entity.Task;
import com.software.levelupandroid.home.adapter.TaskItemAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//Grid设配器
public class GridAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater inflater;
    private Map<Integer, List<Task>> priorityTasksMap; //用于存储不同优先级的任务
    //定义优先级的颜色
    private static final String[] priorityHeaders = {
            "重要且紧急",
            "重要不紧急",
            "紧急不重要",
            "不重要不紧急"
    };
    // 优先级对应的背景颜色值
    private static final int[] priorityColors = {
            0xFFF65D5D, // 红色
            0xFFD3A8FF, // 紫色
            0xFF97D28B, // 绿色
            0xFF85D3F1  // 蓝色
    };

    public GridAdapter(Context context, List<Task> tasks) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        initializePriorityTasksMap(tasks);
    }

    //将不同优先级任务归类
    private void initializePriorityTasksMap(List<Task> tasks) {
        priorityTasksMap = new HashMap<>();
        for (int i = 0; i < 4; i++) {
            priorityTasksMap.put(i, new ArrayList<>());
        }
        if (tasks != null) {
            for (Task task : tasks) {
                priorityTasksMap.get(task.getPriority()).add(task);
            }
        }
    }
    @Override
    public int getCount() {
        return 4; // 因为我们有四个优先级
    }

    @Override
    public Object getItem(int position) {
        return priorityTasksMap.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
//        View gridItem = convertView;
//        if (gridItem == null) {
//            // 为每个网格项填充布局
//            gridItem = LayoutInflater.from(context).inflate(R.layout.grid_item, parent, false);
//        }
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.grid_item, parent, false);
            holder = new ViewHolder();
            holder.priorityHeader = convertView.findViewById(R.id.itemTextView);
            holder.priorityTasks = convertView.findViewById(R.id.itemListView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        // 设置优先级标题
        holder.priorityHeader.setText(priorityHeaders[position]);
        holder.priorityHeader.setBackgroundColor(priorityColors[position]);

        ListViewAdapter listAdapter = new ListViewAdapter(context, priorityTasksMap.get(position));
        holder.priorityTasks.setAdapter(listAdapter);


        //获取TextView和ListView的引用
//        TextView itemTextView = gridItem.findViewById(R.id.itemTextView);
//        ListView itemlistView = gridItem.findViewById(R.id.itemListView);
//        System.out.print(itemTextView.getId());
//
//        //设置TextView的标题
//        itemTextView.setText(titles[position]);
//
//        //设置TextView的背景颜色
//        itemTextView.setBackgroundColor(colors[position]);
//
//        //设置列表
//        ListViewAdapter listAdapter = new ListViewAdapter(context, items[position]);
//        itemlistView.setAdapter(listAdapter);
        return convertView;
    }

    public void updateTask(List<Task> tasks) {
        initializePriorityTasksMap(tasks);
        notifyDataSetChanged();
    }

    private static class ViewHolder {
        TextView priorityHeader;
        ListView priorityTasks;
    }
}
